package stfei.HorizList
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	/**
	 * ...
	 * @author stfei
	 */
	public class Method
	{
		//适配图片大小和匹配容器居中位置
		public static function setPicSize(obj:Bitmap, w:int, h:int){
			if (obj.width > w){
				obj.width = w
				obj.scaleY = obj.scaleX
			}
			if (obj.height > h){
				obj.height = h
				obj.scaleX = obj.scaleY
			}
			obj.x = (w - obj.width) / 2
			obj.y = (h - obj.height) / 2
		}

		//放大图片到固定大小容器
		public static function setBigPicSize(obj:Bitmap, w:int, h:int){
			var otW:int = obj.width
			var otH:int = obj.height
			if (otW > otH){
				if (otW < w){
					obj.width = w
					obj.scaleY = obj.scaleX
				}
			} else if (otW < otH){
				if (otH < h){
					obj.height = h
					obj.scaleX = obj.scaleY
				}
			} else {
				obj.width = w
				obj.scaleY = obj.scaleX
			}
			obj.x = (w - obj.width) / 2
			obj.y = (h - obj.height) / 2
		}

		//使用bitmapData裁剪图片适应容器大小
		public static function setBigPicCup(obj:Bitmap, w:int, h:int) {
			var otW:int = obj.width
			var otH:int = obj.height
			var aw:Number
			var ah:Number
			var ax:Number=0
			var ay:Number=0
			if (otW > otH) {
				ah = h / otH
				aw = (ah * otW) / otW
				ax = (w-(ah * otW))/2
			} else{
				aw = w / otW				
				ah = (aw * otH) / otH
				ay = (h-(aw * otH))/2
			}
			trace(ah , aw,ax,ay)
			var cupBit:BitmapData = new BitmapData(w, h, false, 0x000)
			var re:Rectangle= new Rectangle(0,0,w,h)
			var mtx:Matrix = new Matrix(aw,0,0,ah,ax,ay);

			cupBit.draw(obj,mtx)
			obj.bitmapData = cupBit
		}

	}

}

